package com.android.rapidcash

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import androidx.multidex.MultiDex
import com.blankj.utilcode.util.LogUtils
import com.android.lib.base.baseclass.BaseApplication
import com.android.lib.base.manager.*
import com.android.lib.base.network.error.BaseError
import com.android.lib.base.network.error.ExceptionHandle
import com.android.rapidcash.activitys.LanguageChangeActivity
import com.android.rapidcash.activitys.LoginActivity
import com.android.rapidcash.dialog.TipDialog
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.coroutines.CoroutineExceptionHandler


/**
 * @author mingyan.su
 * @date   2023/2/9 23:19
 * @desc   应用类
 */
class RcApplication : BaseApplication() {


    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(base)
    }

    override fun onCreate() {
        super.onCreate()
        //注册APP前后台切换监听
        appFrontBackRegister()
        // App启动立即注册监听
        registerActivityLifecycle()
        LiveBusManager.liveBusInit()
        MMKVManager.mmkvInit()
        initNetError()

    }

    private fun initNetError() {
        BaseError.setBaseException(CoroutineExceptionHandler { _, throwable ->
            //| 1000  | 成功               |
            //| -1001 | 需要登录或令牌失效 |
            //| -1000 | 失败               |
            //| 其他  | 其他未知错误
            val exception = ExceptionHandle.handleException(throwable)
            when (exception.code) {
                -1000 -> ToastUtils.showShort(exception.errMsg)
                -1001 -> {
                    ToastUtils.showShort(exception.errMsg)
                    if (ActivityManager.top()!! !is LoginActivity){
                        startActivity(Intent(ActivityManager.top()!!, LoginActivity::class.java).apply {
                            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        })
                        ActivityManager.top()?.finish()
                    }

                }
                10006 -> {
//                    TipDialog(ActivityManager.top()!!, "网络错误", "请检查网络设置") {
//                        startActivity(Intent(Settings.ACTION_WIRELESS_SETTINGS).apply {
//                            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//                        })
//                    }.show()
                }
                else -> {
                    ToastUtils.showShort(exception.errMsg)
                }
            }

        })
    }


    /**
     * 注册APP前后台切换监听
     */
    private fun appFrontBackRegister() {
        AppFrontBack.register(this, object : AppFrontBackListener {
            override fun onBack(activity: Activity?) {
                LogUtils.d("onBack")
            }

            override fun onFront(activity: Activity?) {
                LogUtils.d("onFront")
            }
        })
    }

    /**
     * 注册Activity生命周期监听
     */
    private fun registerActivityLifecycle() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStarted(activity: Activity) {
            }

            override fun onActivityDestroyed(activity: Activity) {
                ActivityManager.pop(activity)

            }

            override fun onActivitySaveInstanceState(activity: Activity, p1: Bundle) {
            }

            override fun onActivityStopped(activity: Activity) {
            }

            override fun onActivityCreated(activity: Activity, p1: Bundle?) {
                ActivityManager.push(activity)
            }

            override fun onActivityResumed(activity: Activity) {
            }
        })
    }
}